using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using PropertyExpression.PresentationCode;

namespace WpfApplication1
{
    public partial class Window2
    {
        public Window2()
        {
            InitializeComponent();

            this.SnapsToDevicePixels(true).Nested(
                new Border().Padding(5).Nested(
                    new Grid().RowDefinitions(
                        new RowDefinition().Height_Auto(),
                        new RowDefinition()).Nested(
                        new Grid().ColumnDefinitions(
                            new ColumnDefinition(),
                            new ColumnDefinition().Width(18),
                            new ColumnDefinition()).Nested(
                            Section("Customer", CustomerSection()).Grid_Position(0, 0),
                            Section("Dealer", DealerSection()).Grid_Position(0, 2))
                        )
                    )
                );
        }

        private static Grid Section(string sectionName, UIElement nestedElement)
        {
            return new Grid().VerticalAlignment_Top().RowDefinitions(
                new RowDefinition().Height_Auto(),
                new RowDefinition()).Nested(
                new Border().Grid_Position(0, 0).BorderThickness(1).Padding(10, 5, 10, 4)
                    .Execute(SetFrameBorderBrush)
                    .Background(new SolidColorBrush().Color(Color.FromRgb(128, 128, 128))).Nested(
                    new Grid().Nested(
                        new TextBlock().Text(sectionName).Execute(SetFontStyle).FontWeight_Bold()
                            .FontWeight_Bold().Foreground_White())
                    ),
                new Border().Grid_Position(1, 0).BorderThickness(1, 0, 1, 1)
                    .Execute(SetFrameBorderBrush).Padding(0, 11, 0, 11)
                    .Nested(nestedElement)
                );
        }

        private static Grid CustomerSection()
        {
            return new Grid().RowDefinitions(
                new RowDefinition().Height_Auto(),
                new RowDefinition().Height_Auto(),
                new RowDefinition().Height_Auto(),
                new RowDefinition().Height_Auto(),
                new RowDefinition().Height_Auto(),
                new RowDefinition().Height_Auto(),
                new RowDefinition().Height_Auto()).ColumnDefinitions(
                new ColumnDefinition().Width_Auto(),
                new ColumnDefinition(),
                new ColumnDefinition().Width_Auto(),
                new ColumnDefinition()).Nested(
                TextBlock(0, 0).Text("Name:"), TextBox(0, 1).Grid_ColumnSpan(3),
                TextBlock(1, 0).Text("Contact Name:"), TextBox(1, 1).Grid_ColumnSpan(3),
                TextBlock(2, 0).Text("Phone:"), TextBox(2, 1),
                TextBlock(2, 2).Text("Fax:").HorizontalAlignment_Right(), TextBox(2, 3),
                TextBlock(3, 0).Text("Address:"), TextBox(3, 1).Grid_ColumnSpan(3),
                TextBlock(4, 0).Text("City:"), TextBox(4, 1),
                new Grid().Grid_Position(4, 2).Grid_ColumnSpan(2)
                    .ColumnDefinitions(
                    new ColumnDefinition().Width_Auto(),
                    new ColumnDefinition()
                    ).Nested(
                    TextBlock(0, 0).Text("State/Province:"), TextBox(0, 1)),
                TextBlock(5, 0).Text("ZIP/Postal code:"), TextBox(5, 1),
                TextBlock(5, 2).Text("Country:"), TextBox(5, 3));
        }

        private static Grid DealerSection()
        {
            return new Grid().RowDefinitions(
                new RowDefinition().Height_Auto(),
                new RowDefinition().Height_Auto()).ColumnDefinitions(
                new ColumnDefinition().Width_Auto(),
                new ColumnDefinition()).Nested(
                TextBlock(0, 0).Text("First Contact Name:"), TextBox(0, 0 + 1),
                TextBlock(1, 0).Text("Second Contact Name:"), TextBox(1, 0 + 1));
        }

        private static TextBox TextBox(int row, int column)
        {
            return new TextBox().Grid_Position(row, column)
                .VerticalAlignment_Center().Margin(5, 5, 5, 5)
                .Execute(SetFontStyle).Template(
                new ControlTemplate().Nested(
                    New<Border>().BorderThickness(1).Padding(2)
                        .BorderBrush(new SolidColorBrush().Color(Color.FromRgb(153, 153, 153)))
                        .Nested(New<ScrollViewer>().Name("PART_ContentHost")))
                );
        }

        private static TextBlock TextBlock(int row, int column)
        {
            return new TextBlock().Grid_Position(row, column).Margin(5, 0, 5, 0)
                .MaxWidth(75).TextWrapping_Wrap().Execute(SetFontStyle)
                .VerticalAlignment_Center();
        }

        private static void SetFontStyle(FrameworkElement it)
        {
            it.Style(
                new Style().Setters(
                    new Setter().Control_FontSize(11),
                    new Setter().Control_FontFamily("Tahoma"))
                );
        }

        private static void SetFrameBorderBrush(Border it)
        {
            it.BorderBrush(
                new SolidColorBrush().Color(Color.FromRgb(204, 204, 204))
                );
        }

        protected static IPropertyProvider<T> C<T>()
        {
            return Property.C<T>();
        }

        protected static IElement<T> New<T>()
        {
            return Property.New<T>();
        }
    }
}